/**
 * @(#) ReqInrecMediator.as
 *
 * @date		2012. 11. 07.
 * @version		1.0 
 * @author		Park, Jutae
 * @description	사내추천신청 - Mediator
 * 				사내추천 신청을 조회하고 신청하는 Presentation Logic을 관리 
 */
package com.skt.common_hr.inrec.view
{
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.puremvc.CommonMediator;
	import com.skt.common_hr.inrec.InrecFacade;
	import com.skt.common_hr.inrec.model.InrecProxy;
	import com.skt.common_hr.inrec.model.vo.InrecSaveVO;
	import com.skt.common_hr.inrec.model.vo.InrecVO;
	import com.skt.common_hr.inrec.view.components.ReqInrec;
	
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.controls.TextInput;
	
	import org.puremvc.as3.multicore.interfaces.INotification;	

	public class ReqInrecMediator extends CommonMediator
	{
		public static const NAME:String = "ReqInrecMediator"; 
		
		private var selectedRec:InrecVO;
		private var _selectedRec:InrecSaveVO;
		
		
		/**
		* 생성자
		* 
		* @param
		* @return void
		*/
        public function ReqInrecMediator( viewComponent:Object)
        {
            super( NAME, viewComponent );
        }
 		
		/**
		* View를 초기화 한다.
		*	- View에 기본사항을 출력하고 data를 load
		* @param
		* @return void
		*/
		
		override protected function initializeView():void
 		{
 			setEventListeners();
 		}
 		
 		private function setEventListeners():void
 		{
 			view.addEventListener(ReqInrec.OkInput, onOkInput);
 			view.addEventListener(ReqInrec.CancelInput, onCancelInput);
 		}
 		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Process MVC Messages
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		* 본 Class에서 처리해야할 Noti들을 등록한다.
		* 
		* @param
		* @return void
		*/
		override public function listNotificationInterests():Array 
		{
			return [
					InrecFacade.LOAD_CODES_COMPLETE,
					InrecFacade.LOAD_CODES_FAILED,
					InrecFacade.LOAD_WORKPLACE_COMPLETE,
					InrecFacade.LOAD_WORKPLACE_FAILED,
					InrecFacade.INSERT_COMPLETE,
					InrecFacade.INSERT_FAILED,
					InrecFacade.UPDATE_COMPLETE,
					InrecFacade.UPDATE_FAILED,
					InrecFacade.READ_LIST_COMPLETE
					];
		}
		

		/**
		* Noti별로 처리한다.
		* 
		* @param
		* @return void
		*/
		override public function handleNotification(note:INotification):void
		{
			switch(note.getName()) 
			{
				case InrecFacade.LOAD_CODES_COMPLETE:
					reqList(note.getBody() as InrecProxy);
					break;
				case InrecFacade.LOAD_CODES_FAILED:
					break;
				case InrecFacade.INSERT_COMPLETE:
					facade.sendNotification(InrecFacade.OK_REQUEST_INPUT);
					break;
				case InrecFacade.INSERT_FAILED:
					Alert.show((InrecProxy(note.getBody()).failedAlert));
					facade.sendNotification(InrecFacade.OK_REQUEST_INPUT, this);
					break;	
				case InrecFacade.UPDATE_COMPLETE:
					updateList(note.getBody() as Object);
					break;	
				case InrecFacade.UPDATE_FAILED:
					Alert.show((InrecProxy(note.getBody()).updateFailed));
					break;
				default:
					break;
			}
		}

 		private function onOkInput(event:Event):void
 		{
 			if(view.sDvPop == 'U'){
 				var vo:InrecVO = selectedRec as InrecVO;
 				
 				vo.APPNM = view.tiAPPNM.text;
 				vo.APPOR = view.tiAPPOR.text;
 				vo.APPTE = view.tiAPPTE.text;
 				vo.APPBR = view.df.text;
 				
 				var arrTabT_tabArr:Array = view.vb.getChildren();
		 		var arrTline:Array = [];
		 		
		 		for(var i:int=0; i<arrTabT_tabArr.length; i++) {
					var obj:InrecSaveVO =  new InrecSaveVO();		 			
		 			obj.TLINE = TextInput(arrTabT_tabArr[i]).text;
					arrTline.push(obj);			
		 		}
 				
 				var proxy:InrecProxy = facade.retrieveProxy(InrecProxy.NAME) as InrecProxy;
 				proxy.update(Variables.myPernr, vo, arrTline);
 			} else {
 				var _vo:InrecVO = new InrecVO();	 			
	 			_vo.APPNM = view.tiAPPNM.text;
	 			_vo.APPTE = view.tiAPPTE.text;
	 			_vo.APPOR = view.tiAPPOR.text;
	 			_vo.APPBR = view.df.text;
	 			
		 		var arrTabT_tabArr:Array = view.vb.getChildren();
		 		var arrTline:Array = [];
		 		
		 		for(var i:int=0; i<arrTabT_tabArr.length; i++) {
					var obj:InrecSaveVO =  new InrecSaveVO();		 			
		 			obj.TLINE = TextInput(arrTabT_tabArr[i]).text;
					arrTline.push(obj);			
		 		}
		 		
	 			var proxy1:InrecProxy = facade.retrieveProxy(InrecProxy.NAME) as InrecProxy;
	 			proxy1.insert(Variables.myPernr, _vo, arrTline);	
 			}
 				
 		}
 		
 		private function onCancelInput(event:Event):void
 		{
 			view.removeEventListener(ReqInrec.CancelInput, onCancelInput);
 			view.removeEventListener(ReqInrec.OkInput, onOkInput);
 			
 			facade.sendNotification(InrecFacade.CANCEL_REQUEST_INPUT, this);
 			facade.sendNotification(InrecFacade.OK_REQUEST_INPUT, this);
 		}
 		
 		public var today:Date=new Date();
 		private function reqList(proxy:InrecProxy):void
 		{
 			view.tiAPPNM.text = '';
			view.tiAPPOR.text = '';
			view.tiAPPTE.text = '';
			view.tiAPPNM.enabled = true;
			view.tiAPPTE.enabled = true;
			view.tiAPPOR.enabled = true;
			view.tiLine.enabled = true;
			view.df.enabled = true;
			view.tiLine.text = '';
			view.lbAPPRET.text = '';
			view.btnSave.visible = true;
			view.df.selectedDate = today;
			view.lbAPPSTT.text = '';
			view.vb.removeAllChildren();
			
			var aa:TextInput = view.tiLine;
			view.vb.addChild(aa);
			view.vb.enabled = true;	
 		}
 		
 		private function updateList(rec:Object):void
 		{
 			selectedRec = rec.oSave;
 			
 			view.tiAPPNM.text 	= selectedRec.APPNM;
			view.tiAPPTE.text 	= selectedRec.APPTE;
			view.tiAPPOR.text 	= selectedRec.APPOR;
			view.df.text	  	= selectedRec.APPBR.substring(0, 10);
			view.lbAPPSTT.text  = selectedRec.APPSTT;
			view.lbAPPRET.text = selectedRec.APPRET;
			view.tiAPPNM.enabled = true;
			view.tiAPPTE.enabled = true;
			view.tiAPPOR.enabled = true;
			view.df.enabled = true;
			view.tiLine.enabled = true;
			view.btnSave.visible = true;
			addUpdateTextInput(rec.oSave2);
			view.vb.enabled = true;
			view.vb.verticalScrollPosition = 0;
 		}
 		
 		public function addUpdateTextInput(ac:ArrayCollection):void
 		{
 			var i2:int = view.vb.height;
				view.vb.height = i2;
 			
 			var selectedItem:Object; 
 			for(var i:int=0; i<ac.length; i++){
 				selectedItem = ac.getItemAt(i);
				var tiTextInput:TextInput =  new TextInput();
				tiTextInput.percentWidth = 100;
				tiTextInput.maxChars = 40;
				tiTextInput.setStyle('borderStyle', 'solid');
				tiTextInput.setStyle('borderSides', 'bottom');
				tiTextInput.setStyle('focusThickness', '0');
				tiTextInput.setStyle('color', 'black');
				view.vb.addChildAt(tiTextInput, i);
				tiTextInput.addEventListener(KeyboardEvent.KEY_DOWN, view.keyDownHandlers);
				tiTextInput.addEventListener(KeyboardEvent.KEY_UP, view.keyHandler);
				tiTextInput.text = selectedItem.TLINE;
				
 			}
 		}
 		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Utility Functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
        private function get view():ReqInrec
        {
            return viewComponent as ReqInrec;
        }
		
		
	}
}